<?php
class CounterModel{
	public $table="vicounter";
	public $table_online="vicounter_online";
	public $locktime        =  15; 
	public $initialvalue    =    1;
	public $records        =    100000; 
	public $s_today        =    1; 
	public $s_yesterday    =    1; 
	public $s_all            =    1; 
	public $s_week            =    1; 
	public $s_month        =    1; 
	public $s_digit        =    1; 
	public $disp_type        =     'Mechanical'; 
	public $widthtable        =    '60'; 
	public $pretext; 
	public $posttext; 
	
	public $day;
	public $month;
	public $year;
	public $daystart; 
	public $monthstart; 
	public $weekday; 
	public $weekstart; 
	public function __construct(){
		$this->counter1();
		$this->visiter_online();
	}
	public function visiter_online(){
		$session=session_id();
		$time=time();
		$time_check=$time-30;
		$sql = "SELECT COUNT(*) as `total` FROM ".$this->table_online;
		$query = Db::query($sql);
		$result = Query::fetch($query);
		$total = $result['total'];
		if($total == 0){
			$data = array(
				'session' 			=> $session,
				'time' 				=> $time,						
			);
			Model::insertData($this->table_online, $data);
		}else{
			$data = array(
				'time' 				=> $time,						
			);
			$condition = array(
				'field' => 'session',
				'value' => $session
			);
			Model::updateData($this->table_online, $data, $condition);
		}
		
		$sql='SELECT COUNT(*) as `total` FROM '.$this->table_online.'';
		$query = Db::query($sql);$result = Query::fetch($query);
		$this->online= $result['total'];
		//delete
		$sql='DELETE FROM '.$this->table_online.' WHERE time< :time_check';
		$query = Db::prepare($sql);
		$query->bindParam(':time_check', $time_check);
		$query->execute();
	}
	
	public function show_online(){
		return $this->online;
	}
	public function counter1(){
		$this->locktime        =    $this->locktime * 60; 
		// Now we are checking if the ip was logged in the database. Depending of the value in minutes in the locktime variable. 
		$this->day             =    date('d'); 
		$this->month             =    date('n'); 
		$this->year             =    date('Y'); 
		$this->daystart         =    mktime(0,0,0,$this->month,$this->day,$this->year); 
		$this->monthstart         =  mktime(0,0,0,$this->month,1,$this->year); 
		// weekstart 
		$this->weekday         =    date('w'); 
		$this->weekday--; 
		if ($this->weekday < 0)    $this->weekday = 7; 
		$this->weekday         =    $this->weekday * 24*60*60; 
		$this->weekstart         =    $this->daystart - $this->weekday; 
	
		$this->yesterdaystart     =    $this->daystart - (24*60*60); 
		$now             =    time(); 
		$ip                 =    $_SERVER['REMOTE_ADDR']; 
		 
		$sql             =    "SELECT MAX(count_id) AS total FROM ".$this->table; 
		$query = Db::query($sql);
		$t = Query::fetch($query);
		$all_visitors     =    $t['total']; 
		 
		if ($all_visitors !== NULL) { 
			$all_visitors += $this->initialvalue; 
		} else { 
			$all_visitors = $this->initialvalue; 
		} 
		 
		// Delete old records 
		$temp = $all_visitors - $this->records; 
		 
		if ($temp>0){ 
			$sql         =  "DELETE FROM ".$this->table." WHERE `count_id`<'$temp'"; 
			$query = Db::query($sql);
		} 
		$sql             =    "SELECT COUNT(*) AS visitip FROM ".$this->table." WHERE ip='$ip' AND (tm+'$this->locktime')>'$now'"; 
		$query = Db::query($sql);
		$vip  = Query::fetch($query);
		$items             =    $vip['visitip']; 
		if (empty($items)) 
		{ 
			$sql = "INSERT INTO ".$this->table." (tm, ip) VALUES ('$now', '$ip')"; 
			$query = Db::query($sql);
		}
	}
	public function show_today(){
		$sql="SELECT COUNT(*) as total FROM $this->table WHERE tm>'$this->daystart'";
		$query = Db::query($sql);
		$result = Query::fetch($query);
		$total = $result['total'];
		return $total;
	}
	public function show_yesterday(){
		$sql="SELECT COUNT(*) as total FROM $this->table WHERE tm>'$this->yesterdaystart' and tm<'$this->daystart'";
		$query = Db::query($sql);
		$result = Query::fetch($query);
		$total = $result['total'];
		return $total;	
	}
	public function show_weekday(){
		$sql="SELECT COUNT(*) as total FROM $this->table WHERE  tm>='$this->weekstart'";
		$query = Db::query($sql);
		$result = Query::fetch($query);
		$total = $result['total'];
		return $total;
	}
	public function show_month(){
		$sql="SELECT COUNT(*) as total FROM $this->table WHERE  tm>='$this->monthstart'";
		$query = Db::query($sql);
		$result = Query::fetch($query);
		$total = $result['total'];
		return $total;
	}
	public function show_all(){
		$sql="SELECT MAX(count_id) AS total FROM ".$this->table; 
		$query = Db::query($sql);
		$result = Query::fetch($query);
		return $result['total'];
	}
}